<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-form-item>
          <el-input v-model="filters.name" clearable placeholder="标签名称"/>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.shop_name" clearable placeholder="商铺名称"/>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.sortno" clearable placeholder="序号"/>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="dateTime"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <kt-button
            icon="fa fa-search"
            :label="$t('action.search')"
            type="primary"
            @click="findPage(null)"/>
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-plus"
            :label="$t('action.add')"
            type="primary"
            @click="handleAdd"
          />
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
        </el-form-item>
      </el-form>
    </div>

    <!--表格内容栏-->
    <cy-table
      :height="350"
      :highlightCurrentRow="true"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      ref="CyTable">
    </cy-table>

    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false">
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商铺名称" prop="shop_id">
          <el-select v-model="dataForm.shop_id" placeholder="所属店铺" style="width: 100%">
            <el-option
              v-for="item in optionShop"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="dataForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="sortno">
          <el-input v-model="dataForm.sortno" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="describes">
          <el-input v-model="dataForm.describes" auto-complete="off" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import CyTable from "@/views/Core/CyTable";
  import KtButton from "@/views/Core/KtButton";
  import { format, formatDate } from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  export default {
    components: {
      KtButton,
      CyTable,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: "small",
        filters: {
          end_time: "",
          start_time: "",
          sortno: "",
          shop_name: "",
          name: "",
          t: "productTag"
        },
        optionShop: [],
        columns: [
          {prop: "id", label: "ID", minWidth: 50},
          {prop: "name", label: "标签名称", minWidth: 120},
          {prop: "shop_name", label: "商铺名称", minWidth: 120},
          {prop: "sortno", label: "序号", minWidth: 120},
          {prop: "describes", label: "描述", minWidth: 120},
          {prop: "create_time", label: "创建时间", minWidth: 120},
        ],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},
        filterColumns: [],
        editLoading: false,
        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        dataFormRules: {
          name: [{required: true, message: "请输入标签名称", trigger: "blur"}],
          shop_id: [{required: true, message: "请输入所属店铺", trigger: "blur"}],
          start: [{required: true, message: "请输入起始价格", trigger: "blur"}],
          end: [{required: true, message: "请输入结束价格", trigger: "blur"}],
        },
        // 新增编辑界面数据
        dataForm: {
          id: "",
          shop_id: "",
          name: "",
          sortno: "",
          describes: "",
        },
        // 时间格式
        dateTime: "",
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      };
    },
    mounted() {
      this.initColumns();
      this.queryTypeList();
    },
    methods: {
      // 查询店铺
      queryTypeList() {
        let _this = this;
        this.utils.request.queryShop(null,function(res) {
          _this.optionShop = res.data;
        })
      },
      // 获取分页数据
      findPage: function (data) {
        this.filters.t = "productTag";
        if (this.dateTime !== '') {
          this.filters.start_time = formatDate(this.dateTime[0], "yyyy-MM-dd");
          this.filters.end_time = formatDate(this.dateTime[1], "yyyy-MM-dd");
        }
        this.$refs.CyTable.findPage(this.filters);
      },
      // 批量删除
      handleDelete: function (data) {
        var ids = '';
        for (var i = 0; i < data.params.length; i++) {
          ids = ids + data.params[i].id + ',';
        }
        data.t = 'productTag'
        data.ids = ids
        this.utils.request.deleteUserInfo(data, data.callback)
      },
      // 显示新增界面
      handleAdd: function() {
        this.editDialogVisible = true;
        this.operation = true;
        if (this.dataForm.id != '') {
          this.dataForm.sortno = "";
          this.dataForm.shop_id = "";
          this.dataForm.creator = "";
          this.dataForm.update_time = "";
          this.dataForm.create_time = "";
          this.dataForm.name = "";
          this.dataForm.updator = "";
          this.dataForm.id = "";
          this.dataForm.shop_name = "";
          this.dataForm.describes = "";
          this.dataForm.is_delete = "";
        }
      },
      // 显示编辑界面
      handleEdit: function(params) {
        this.editDialogVisible = true;
        this.operation = false;
        this.$nextTick(function() {
          this.dataForm = Object.assign({}, params.row);
        });
      },
      // 编辑
      submitForm: function() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm("确认提交吗？", "提示", {}).then(() => {
              let params = Object.assign({}, this.dataForm);
              params.t = "productTag";
              var this_ = this;
              this.utils.request.editInfo(params, function(res) {
                if (res.code == "0000") {
                  this_.$message({ message: "操作成功", type: "success" });

                  this_.$refs["dataForm"].resetFields();

                  this_.editDialogVisible = false;

                  this_.findPage(null);
                } else {
                  this_.$message({
                    message: "操作失败, " + res.msg,
                    type: "error"
                  });
                }
              });
            });
          }
        });
      },
      // 时间格式化
      dateFormat: function(row, column, cellValue, index) {
        return format(row[column.property]);
      },
      // 重置
      resetForm(formName) {
        this.filters = {
          name: "",
          shop_id: "",
          status: "",
          start_time: "",
          end_time: ""
        };
        this.dateTime = "";
        this.findPage();
      },
      // 处理表格列过滤显示
      initColumns: function () {
        var temp = [];
        $.each(this.columns, function (key, val) {
          temp.push(val)
        })
        this.filterColumns = temp;
      },
    }
  };
</script>

<style scoped>

</style>
